#include <iostream>
#include <string>
#include <algorithm>
#include <map>

#define MAX_PROJECTS    100

struct project
{
    int id;             // project id
    std::string name;   // project name
    int members;        // number of members
};

static bool project_comparer(const project &a, const project &b)
{
    return (a.members > b.members) 
        || (a.members == b.members && a.name < b.name);
}

static bool solve()
{
    std::string line;
    int project_id = 0;
    project projects[MAX_PROJECTS+1];   // projects[0] is not used
    std::map<std::string, int> signup;  // student name => project id (or -1)

    while (std::getline(std::cin, line))
    {
        if (line.size() == 0)
            continue;
        else if (line[0] == '0')
            return false;
        else if (line[0] == '1') // finish case; print summary
            break;
        else if (line[0] >= 'A' && line[0] <= 'Z') // project name
        {
            project_id++;
            projects[project_id].id = project_id;
            projects[project_id].name = line;
            projects[project_id].members = 0;
        }
        else // student name
        {
            int &id = signup[line];
            if (id == 0) // new member
            {
                id = project_id;
                ++projects[project_id].members;
            }
            else if (id > 0 && id != project_id) // already signed up for 'id'
            {
                --projects[id].members;
                id = -1;
            }
        }
    }
        
    std::sort(projects + 1, projects + project_id + 1, project_comparer);
    for (int i = 1; i <= project_id; i++)
    {
        std::cout << projects[i].name << ' ' << projects[i].members << '\n';
    }
    return true;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    while (solve());
    return 0;
}
